package com.winning.sjzc.sjygl.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.github.pagehelper.PageInfo;
import com.winning.sjzc.common.utils.ResponseEntity;
import com.winning.sjzc.sjygl.entity.SJYXX;
import com.winning.sjzc.sjygl.entity.ZdbEntity;
import com.winning.sjzc.sjygl.service.ElementManageService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/element")
public class ElementManageController {

    private static final Logger logger = LogManager.getLogger(ElementManageController.class);

    @Autowired
    private ElementManageService elementManageService;

    //查询
    @RequestMapping(value = "/queryElementList", method = RequestMethod.POST)
    public @ResponseBody
    String queryElementList(Integer limit, Integer offset, String sjymc){
        PageInfo<SJYXX> page=new PageInfo<>();
        try{
            page=elementManageService.queryElementList(offset,limit,sjymc);
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            return "";
        }
        List<SJYXX> list = page.getList();
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
        JSONObject result= new JSONObject();
        result.put("total",page.getTotal());
        result.put("rows",array);
        return JSON.toJSONString(result,new PascalNameFilter())
                .replace("Total","total")
                .replace("Rows","rows");
    }

    //删除
    @RequestMapping(value = "/deleteElementBySjydm", method = RequestMethod.GET)
    public @ResponseBody
    String deleteElementBySjydm(String sjydm){
        JSONObject resObj = new JSONObject();
        try {
            resObj.put("code", "200");
            resObj.put("repMsg", "操作成功！");
            elementManageService.deleteElementBySjydm(sjydm);
        }catch (Exception e){
            resObj.put("code", "500");
            resObj.put("repMsg", e.getMessage());
            logger.error(e.getMessage(), e);
        }
         return resObj.toJSONString();
    }

    //新增
    @RequestMapping(value = "/saveElement", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity saveElement(HttpServletRequest request, @RequestBody SJYXX sjyxx) {
        int res = -1;
        try{
            res = elementManageService.saveElement(sjyxx);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.error();
        }
        return ResponseEntity.ok().put("res",res);
    }

    //修改
    @RequestMapping(value = "/updateElement", method = RequestMethod.POST)
    public @ResponseBody
    ResponseEntity updateElement(HttpServletRequest request, @RequestBody SJYXX sjyxx) {
        int res = -1;
        try{
            res = elementManageService.updateElement(sjyxx);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseEntity.error();
        }
        return ResponseEntity.ok().put("res",res);
    }

    //编辑回显
    @RequestMapping(value = "/querySjyxxBySjydm", method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity querySjyxxBySjydm(String sjydm){
        SJYXX sjyxx= elementManageService.querySjyxxBySjydm(sjydm);
        return ResponseEntity.ok().put("sjyxx",sjyxx);
    }

    //初始化标准来源和数据元类型
    @RequestMapping(value = "/initAddData", method = RequestMethod.GET)
    public @ResponseBody
    ResponseEntity initAddData(String zblbdm){
        if(StringUtils.isBlank(zblbdm)){
            return null;
        }
        List <ZdbEntity> list= elementManageService.initAddData(zblbdm);
        return ResponseEntity.ok().put("zdbList",list);
    }

}
